package com.wsh.work.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsh.base.BaseVo;
import com.wsh.dto.work.StudentTestDto;
import com.wsh.entity.work.StudentTestEntity;
import com.wsh.mapper.work.StudentTestDao;
import com.wsh.sys.FileService;
import com.wsh.utils.PageUtils;
import com.wsh.vo.work.StudentTestVo;
import com.wsh.work.StudentTestService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author 王思涵
* @date 2020-02-29
*/
@Service
public class StudentTestServiceImpl extends ServiceImpl<StudentTestDao, StudentTestEntity> implements StudentTestService {

    @Autowired
    private FileService fileService;
    @Override
    public IPage<StudentTestEntity> getList(StudentTestDto studentTestDto) {
        // 分页数据处理
        IPage<StudentTestEntity> pages = PageUtils.getPage(studentTestDto);

        String name = studentTestDto.getName();
        Integer sex = studentTestDto.getSex();
        String beforeBirthday = studentTestDto.getBeforeBirthday();
        String afterBirthday = studentTestDto.getAfterBirthday();

        // 分页查询处理
        IPage<StudentTestEntity> page = baseMapper.selectPage(pages, new QueryWrapper<StudentTestEntity>()
                .like(StringUtils.isNotBlank(name), "name", name)
                .eq(sex != null, "sex", sex)
                .between(StringUtils.isNotBlank(beforeBirthday) && StringUtils.isNotBlank(afterBirthday), "birthday", beforeBirthday, afterBirthday)
        );

        return page;
    }

    @Override
    public BaseVo<StudentTestVo> queryVo(String formId) {
        StudentTestVo studentTestvo = new StudentTestVo();

        StudentTestEntity studentTestEntity = getById(formId);

        BeanUtils.copyProperties(studentTestEntity, studentTestvo);
        List<Map<String, Object>> fileLists = fileService.getListByFormId(formId, true);
        return new BaseVo<>(studentTestvo, fileLists);

    }

    @Override
    public boolean saveStudentTest(StudentTestDto studentTestDto) {
        StudentTestEntity studentTestEntity = new StudentTestEntity();

        BeanUtils.copyProperties(studentTestDto, studentTestEntity);

        boolean saveEntity = saveOrUpdate(studentTestEntity);

        boolean saveFile = fileService.uploadFormId(studentTestDto.getNewFileList(), studentTestDto.getFileRemoveIds(), studentTestEntity.getId());
        return saveEntity && saveFile;
    }
}
